package com.zx.work1.Dao;

import com.zx.work1.bean.Student;
import com.zx.work1.util.DbUtil;
import com.zx.work1.util.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.*;
import java.util.Arrays;
import java.util.List;

public class StudentDaoImp implements StudentDao {
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    @Override
    public Student addStudent(Student student) {
        try {
            // 1.获取数据库连接
            QueryRunner qr = new QueryRunner();
            // 2.准备sql语句
            String sql = "INSERT INTO t_student(num,name,sex,data,email,remark) VALUES(?,?,?,?,?,?)";
            Object[] param = {student.getNum(),student.getName(),student.getSex(),student.getData(),student.getEmail(),student.getRemark()};
            System.out.println("名字是："+student.getName());
            connection = DruidUtils.getConnection();
             int i = qr.update(connection,sql,param);
             if(i != 0) {
                 return student;
             }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 4.释放相关的资源
            try {
                DbUtil.closeResource(connection, preparedStatement);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return  null; // 表示查找失败
    }

    @Override
    public Student delStudent(Student student) {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        String sql = "delete from t_student where num = ?";

        int i = student.getNum();
        //如果只有一个参数的话 不需要创建数组
        try {
            qr.update(sql,i);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Student modifyStudent(Student student) {
            //1.创建 核心类 自动模式 需要传递 数据库连接池对象
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            // 2.准备sql语句
            String sql = "UPDATE t_student set num = ? , name = ? , sex = ? , data = ? , email = ? , remark = ? WHERE num = ?";
            Object[] param = {student.getNum(),student.getName(),student.getSex(),student.getData(),student.getEmail(),student.getRemark(),student.getNum()};

            //3.执行修改操作 自动模式不需要传入connection对象
        try {
            qr.update(sql,param);
            System.out.println("修改成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  null; // 表示查找失败
    }

    @Override
    public Student queryStudentNum(int num) {
        //1.创建QueryRunner
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        //2.编写SQL
        String sql = "select * from t_student where num = ?";
        Student stu = null;
        //3.执行查询
        try {
            stu = qr.query(sql, new BeanHandler<Student>(Student.class), num);
            return stu;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Object[]> queryStudent() {
        //1.创建QueryRunner
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        //2.编写SQL
        String sql = "select * from t_student";

        //3.执行查询
        List<Object[]> query = null;
        try {
            query = qr.query(sql, new ArrayListHandler());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
        //4.遍历集合获取数据
//        for (Object[] objects : query) {
//            System.out.println(Arrays.toString(objects));
//        }

    }
}
